﻿using System;
using Microsoft.Phone.Controls;

namespace WP7AppLifecycleServiceApp
{
    /// <summary>
    /// Class for the Start page in the application.
    /// </summary>
    public partial class StartPage : PhoneApplicationPage, WP7AppLifecycleService.IPageStateHandler
    {
        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="StartPage"/> class.
        /// </summary>
        public StartPage()
        {
            InitializeComponent();
            WP7AppLifecycleService.WP7AppLifecycleApplicationService.Current.RegisterPageStateHandler(this, this);
        }

        #endregion Constructor
        
        #region interface IPageStateHandler

        /// <summary>
        /// Saves the page state when the application is tombstoned.
        /// </summary>
        /// <param name="state">A <see cref="System.Collections.Generic.IDictionary{String, Object}"/> instance
        /// that can be used to store the page state.</param>
        /// <remarks>It would be desirable to be able to use PhoneApplicationPage.State in the implementation
        /// of methods, but due to implementation details when data needs to be restored it won´t be available.
        /// </remarks>
        public void SavePageState(System.Collections.Generic.IDictionary<string, object> state)
        {
            if (null == state)
            {
                throw new ArgumentNullException("state");
            }

            state["StartPage.selectedItem"] = listBox.SelectedItem;
        }

        /// <summary>
        /// Restores the data of the page when the application comes back to life
        /// after having been tombstoned.
        /// </summary>
        /// <param name="state">A <see cref="System.Collections.Generic.IDictionary{String, Object}"/> instance
        /// that can be used to restore the page state.</param>
        /// <remarks>PhoneApplicationPage.State is not available.</remarks>
        public void RestorePageState(System.Collections.Generic.IDictionary<string, object> state)
        {
            if (null == state)
            {
                throw new ArgumentNullException("state");
            }

            if (state.ContainsKey("StartPage.selectedItem"))
            {
                listBox.SelectedItem = state["StartPage.selectedItem"];
            }
        }

        /// <summary>
        /// Deletes any state saved when the application was tombstoned. 
        /// <para>This method will automatically be called right after RestoreApplicationState is
        /// successfully executed.</para>
        /// </summary>
        /// <param name="state">A <see cref="System.Collections.Generic.IDictionary{String, Object}"/> instance
        /// that can be used to store the page state.</param>
        /// <remarks>PhoneApplicationPage.State is not available.</remarks>
        public void DeletePageState(System.Collections.Generic.IDictionary<string, object> state)
        {
            if (null == state)
            {
                throw new ArgumentNullException("state");
            }

            if (state.ContainsKey("StartPage.selectedItem"))
            {
                state.Remove("StartPage.selectedItem");
            }
        }

        #endregion interface IPageStateHandler
    }
}